Ext.define('BISC.view.AttributeWindow', {
  extend: 'Ext.window.Window',

  height: 144,
  id: 'attributeWindow',
  width: 360,
  title: 'Image Attributes',

  initComponent: function() {
    var me = this;

    Ext.applyIf(me, {
      dockedItems: [
        {
          xtype: 'toolbar',
          height: 57,
          width: 349,
          dock: 'bottom',
          layout: {
            align: 'stretch',
            padding: 5,
            type: 'hbox'
          },
          items: [
            {
              xtype: 'tbfill',
              width: 229
            },
            {
              xtype: 'button',
              autoShow: true,
              frame: false,
              width: 53,
              iconAlign: 'right',
              scale: 'large',
              text: 'OK',
              listeners: {
                click: {
                  fn: me.onButtonClick1,
                  scope: me
                }
              }
            },
            {
              xtype: 'button',
              text: 'Cancel',
              listeners: {
                click: {
                  fn: me.onButtonClick,
                  scope: me
                }
              }
            }
          ]
        }
      ],
      items: [
        {
          xtype: 'combobox',
          id: 'categories',
          readOnly: false,
          fieldLabel: 'Categories',
          displayField: 'title',
          forceSelection: true,
          store: 'CategoriesStore',
          valueField: 'typeId'
        },
        {
          xtype: 'combobox',
          id: 'attributes',
          fieldLabel: 'Attributes',
          editable: false,
          displayField: 'name',
          forceSelection: true,
          store: 'AttributesStore',
          valueField: 'valueId'
        }
      ]
    });

    me.callParent(arguments);
  },

  onButtonClick1: function(button, e, options) {
    //console.log(e);
    //console.log(options);
    //console.log(button);

    var categoryId = options.scope.items.items[0].getValue();
    var attributeId = options.scope.items.items[1].getValue();
    Ext.data.JsonP.request({url: 'http://localhost:8888/images/attribute/add',                          
      timeout: 120000,
      scope: this,
      params: {
        bisId: 1,
        categoryId: categoryId,
        attributeId: attributeId,
        imageId: 1
      },
      success: function(responseObject) {
        // console.log(responseObject);

        if(responseObject.success) {
          alert('Attribute Added');
        } else {
          alert('Attribute Not Added');
        }
      },
      failure: function() {
        alert('In Ajax failure function');
      }
    });
  },

  onButtonClick: function(button, e, options) {
    this.hide();
  }

});
